package com.sikulifang.bank.simulation.controller;

import cn.hutool.db.Entity;
import com.sikulifang.bank.simulation.dto.QueryCriteriaDTO;
import com.sikulifang.bank.simulation.service.IService;
import com.sikulifang.bank.simulation.util.R;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * ClassName AbstractController
 * Description AbstractController
 *
 * @author Yat
 * Date 2023/6/8 17:32
 * version 1.0
 */
@Slf4j
public abstract class AbstractController<S extends IService> {

    @Getter
    @Autowired(required = false)
    public S service;

    @SneakyThrows
    @GetMapping("/get")
    protected Entity get(String id) {
        return service.get(id);
    }

    @SneakyThrows
    @DeleteMapping("/delete/{id}")
    protected R<String> delete(@PathVariable String id) {
        service.delete(id);
        return R.ok();
    }

    @SneakyThrows
    @PostMapping("/list")
    protected Entity list(@RequestBody QueryCriteriaDTO queryCriteria) {
        return service.list(queryCriteria);
    }

    @PostMapping("/saveUpdate")
    public R<String> saveUpdate(@RequestBody Entity entity) {
        return service.saveUpdate(entity);
    }
}
